package com.project.psc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.psc.bean.GoodsBean;
import com.project.psc.bean.SupplyBean;
import com.project.psc.bean.SupplyGoodsBean;
import com.project.dto.GoodsToCscDto;
import com.project.dto.SupplyToCscDto;
import com.project.psc.mapper.IGoodsMapper;
import com.project.psc.mapper.ISupplyGoodsMapper;
import com.project.psc.mapper.ISupplyMapper;
import com.project.psc.service.ISupplyService;
import com.project.psc.vo.AddSupplyVo;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SupplyServiceImpl implements ISupplyService {
    @Autowired
    private ISupplyMapper supplyMapper;
    @Autowired
    private IGoodsMapper goodsMapper;
    @Autowired
    private ISupplyGoodsMapper supplyGoodsMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void updateStatus(String batch, Integer status) {
        QueryWrapper<SupplyBean> qw=new QueryWrapper<>();
        qw.eq("s_batch",batch);
        SupplyBean supplyBean = supplyMapper.selectOne(qw);
        supplyBean.setStatus(status);
        supplyMapper.updateById(supplyBean);
    }

    @Override
    public void addSupply(AddSupplyVo addSupplyVo, Long supplierId, String supplierName) {
        SupplyBean supply = ConvertBeanUtil.copy(addSupplyVo, SupplyBean.class);
        GoodsBean[] goodsArr= JSONObject.parseObject(addSupplyVo.getGoodsArr(),GoodsBean[].class);
        String batch="P"+System.currentTimeMillis();
        supply.setBatch(batch);
        supply.setSupplierId(supplierId);
        supply.setSupplierName(supplierName);
        supplyMapper.insert(supply);
        //传给审核的商品集合
        List<GoodsToCscDto> goodsList=new ArrayList<>();
        for (int i = 0; i < goodsArr.length; i++) {
            GoodsBean goodsBean = goodsArr[i];
            goodsBean.setSupplyId(supply.getId());
            goodsMapper.insert(goodsBean);
            //传给审核系统的
            SupplyGoodsBean supplyGoodsBean = supplyGoodsMapper.selectById(goodsBean.getGoodsId());
            GoodsToCscDto goodsToCscDto = ConvertBeanUtil.copy(supplyGoodsBean, GoodsToCscDto.class);
            goodsToCscDto.setSum(goodsBean.getNum());
            goodsList.add(goodsToCscDto);
        }


        //发送给审核系统
        SupplyToCscDto supplyToCscDto=new SupplyToCscDto();
        supplyToCscDto.setSupplyRef(supply.getId());
        supplyToCscDto.setSupplierRef(supply.getSupplierId());
        supplyToCscDto.setPact(supply.getPact());
        supplyToCscDto.setBatch(supply.getBatch());
        supplyToCscDto.setSupplierName(supply.getSupplierName());
        supplyToCscDto.setAddress(supply.getAddress());
        supplyToCscDto.setStatus(supply.getStatus());
        supplyToCscDto.setMoney(supply.getSumPrice());
        supplyToCscDto.setDateTime(supply.getTime());
        supplyToCscDto.setSupplyGoodsEntityList(goodsList);
        String json= JSON.toJSONString(supplyToCscDto);
        rabbitTemplate.convertAndSend("supplierOne","supplier.sendSupply",json);
    }
}
